//
//  LHNetworking.m
//  lhcx
//
//  Created by 赵永杰 on 2017/5/15.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "LHNetworking.h"
#import <AFNetworking/AFNetworking.h>
#import <HLBaseLibraries/HLBaseLibraries.h>

@implementation LHNetworking

+ (instancetype)shared {
    static LHNetworking *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[LHNetworking alloc] init];
    });
    return shared;
}

static AFHTTPSessionManager *manager ;

- (AFHTTPSessionManager *)sharedHTTPSession {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        manager.requestSerializer.timeoutInterval = 10;
    });
    return manager;
}

- (void)GET:(NSString *)URLString
 parameters:(id)parameters
    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
    failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    AFHTTPSessionManager *manager = [self sharedHTTPSession];
    [manager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task, responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(task, error);
    }];
    
}

- (void)POST:(NSString *)URLString
  parameters:(id)parameters
     success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
     failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    // 设置参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:parameters];
    [params setValue:@"0" forKey:@"source"];
    [params setValue:LHGetUserToken() forKey:@"token"];
    [params setValue:LHGetUserID() forKey:@"userId"];
    AFHTTPSessionManager *manager = [self sharedHTTPSession];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//    [manager.requestSerializer setValue:LHGetUserToken() forHTTPHeaderField:@"Token"];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [manager POST:URLString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [HLProgressHUD dismissHUD];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

        NSInteger code = [[responseObject valueForKey:@"code"] integerValue];
        if (code == 1) {
            success(task, responseObject);
        }else{
            success(task, nil);
            if (code != 3000) {
                [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleError status:responseObject[@"message"]];
            }
            
            [[NSNotificationCenter defaultCenter] postNotificationName:LHErrorCodeMethod object:[NSString stringWithFormat:@"%ld",(long)code]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [HLProgressHUD dismissHUD];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        failure(task, error);
    }];
}

- (void)POSTNoToken:(NSString *)URLString
         parameters:(id)parameters
            success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
            failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    // 设置参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:parameters];
//    [params setValue:@"0" forKey:@"source"];
    AFHTTPSessionManager *manager = [self sharedHTTPSession];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [manager POST:URLString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [HLProgressHUD dismissHUD];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        NSInteger code = [[responseObject valueForKey:@"code"] integerValue];
        if (code == 1) {
            success(task, responseObject);
        }else{
            success(task, nil);
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleError status:responseObject[@"message"]];
            
            [HLNoteCenter postNotificationName:LHErrorCodeMethod object:[NSString stringWithFormat:@"%ld",(long)code]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [HLProgressHUD dismissHUD];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        failure(task, error);
    }];
}

@end
